/*
 *  Copyright (c) 2011 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU Lesser Public License v2.1
 *  which accompanies this distribution, and is available at
 *  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 *  Contributors:
 *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - RMI Client, FileChooser and WebDAV
 */
package de.ipk_gatersleben.bit.bi.edal.publication;

import java.rmi.ConnectException;
import java.rmi.RemoteException;

import javax.mail.internet.InternetAddress;
import javax.security.auth.Subject;

import de.ipk_gatersleben.bit.bi.edal.primary_data.EdalConfiguration;
import de.ipk_gatersleben.bit.bi.edal.rmi.client.ClientDataManager;
import de.ipk_gatersleben.bit.bi.edal.rmi.server.Authentication;
import de.ipk_gatersleben.bit.bi.edal.rmi.server.EdalServer;
import de.ipk_gatersleben.bit.bi.edal.sample.EdalHelpers;

public class PublicationModul {

	// private static class ShutdownInterceptor extends Thread {
	//
	// @Override
	// public void run() {
	// System.out.println("eDAL-WEBSTART: JVM got external kill signal - graceful shut down");
	// try {
	// EdalServer.stopServer("localhost", EdalServer.DEFAULT_REGISTRY_PORT);
	// } catch (RemoteException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// }
	
	private static final String SERVER = "localhost";

	private static PublicationFrame frame;

	public static ClientDataManager clientDataManager;

	public static PublicationFrame getFrame() {
		return PublicationModul.frame;
	}

	private static void setCurrentFrame(PublicationFrame frame) {
		PublicationModul.frame = frame;
	}

	public static void main(String[] args) throws Exception {

		EdalConfiguration configuration = new EdalConfiguration("dummy",
				"dummy", "10.5072", new InternetAddress(
						"arendd@ipk-gatersleben.de"), new InternetAddress(
						"arendd@ipk-gatersleben.de"), new InternetAddress(
						"arendd@ipk-gatersleben.de"), new InternetAddress(
						"eDAL0815@ipk-gatersleben.de"));

		EdalServer.startServer(configuration, EdalServer.DEFAULT_REGISTRY_PORT,
				EdalServer.DEFAULT_DATA_PORT, false, false);

		// Runtime.getRuntime().addShutdownHook(new ShutdownInterceptor());

		PropertyLoader.main(null);

		/* test connect to eDAL server */

		Authentication testAuthentication = new Authentication(
				EdalHelpers.authenticateSampleUser());

		Boolean retry = true;
		while (retry) {
			try {
				clientDataManager = new ClientDataManager(
						EdalServer.DEFAULT_REGISTRY_PORT, SERVER,
						testAuthentication);

				clientDataManager.getRootDirectory();
				retry = false;
			} catch (ConnectException e) {

				ServerErrorDialog dialog = new ServerErrorDialog(null,
						e.getMessage());

				dialog.showDialog();

				if (dialog.getReturnValue() == 1) {
					retry = true;
				} else {
					System.exit(0);
				}
			}
		}

		Subject ipkSubject = new IpkAuthenticationProcess().getSubject();

		if (ipkSubject != null) {
			Authentication authentication = new Authentication(ipkSubject);

			retry = true;

			while (retry) {
				try {
					clientDataManager = new ClientDataManager(
							EdalServer.DEFAULT_REGISTRY_PORT,SERVER,
							authentication);

					PublicationFrame.rootDirectory = clientDataManager
							.getRootDirectory();
					retry = false;
				} catch (ConnectException e) {

					ServerErrorDialog dialog = new ServerErrorDialog(null,
							e.getMessage());

					dialog.showDialog();

					if (dialog.getReturnValue() == 1) {
						retry = true;
					} else {
						EdalServer.stopServer(SERVER,
								EdalServer.DEFAULT_DATA_PORT);

						System.exit(0);
					}
				}
			}

			PublicationModul.setCurrentFrame(new PublicationFrame(
					authentication.getName()));
		} else {
			System.exit(0);
		}

	}
}
